package com.yswy.backstage.controller.api;


import com.yswy.backstage.annotation.NotAuth;
import com.yswy.backstage.serviceApi.ApiTgCallBackJlIdService;
import com.yswy.backstage.serviceApi.ApiTgCallBackKsIdService;
import com.yswy.domain.vo.dto.TgDeviceKs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/call")
@Api(tags = "推广回传相关")
@Slf4j
public class ApiTgCallBackController {

    @Autowired
    private  ApiTgCallBackJlIdService apiTgCallBackJlIdService ;
    @Autowired
    private  ApiTgCallBackKsIdService apiTgCallBackKsIdService ;

    /** 推广相关 **/
    @GetMapping("/jlCallBack")
    @ApiOperation(value = "推广相关")
    @NotAuth
    public void jlCallBack(

            @RequestParam(value = "type" ,required = false ,defaultValue = "0") Integer type ,
            @RequestParam(value = "channel" ,required = false) String channel ,
            @RequestParam(value = "packChannel" ,required = false) String packChannel ,
            @RequestParam(value = "projectId" ,required = false) Long projectId ,
            HttpServletRequest request, HttpServletResponse response
    ) throws IOException {
//       https://aiimg.zyykj168.com/tg/handleCallback?&UA=__UA__&type=2&channle=def&ADVERTISER_ID=__ADVERTISER_ID__&CSITE=__CSITE__&CONVERT_ID=__CONVERT_ID__&IMEI=__IMEI__&ANDROIDID=__ANDROIDID__&OAID=__OAID__&CALLBACK_PARAM=__CALLBACK_PARAM__&OS=__OS__&MAC=__MAC__&IP=__IP__&MODEL=__MODEL__
//        https://aiimg.zyykj168.com/tg/handleCallback?
//        type=1
//        &channle=def
//        &ADVERTISER_ID=__ADVERTISER_ID__
//        &CSITE=__CSITE__
//        &CONVERT_ID=__CONVERT_ID__
//        &IMEI=__IMEI__
//        &ANDROIDID=__ANDROIDID__
//        &OAID=__OAID__
//        &CALLBACK_PARAM=__CALLBACK_PARAM__
//        &OS=__OS__
//        &MAC=__MAC__
//        &IP=__IP__
//        &MODEL=__MODEL__
//        Enumeration params = request.getParameterNames();
//        while(params.hasMoreElements()){
//            String paramName = (String)params.nextElement();
//            System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName));
//        }
        log.info("推广相关channle = "+channel+" type = "+type);
        apiTgCallBackJlIdService.addTgCallBackJlId(channel ,packChannel,projectId,request  ,type );
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("success");
        response.getWriter().flush();
        response.getWriter().close();
    }


    /** 推广相关 **/
    @GetMapping("/ksCallback")
    @ApiOperation(value = "推广相关(快手)")
    @NotAuth
    public void ksCallback(
            @RequestParam(value = "channel" ,required = false ,defaultValue = "defChannle") String channel ,
            @RequestParam(value = "packChannel" ,required = false) String packChannel ,
            @RequestParam(value = "projectId" ,required = false) Long projectId ,
            @RequestParam(value = "DID" ,required = false) String DID,
            @RequestParam(value = "AID" ,required = false) String AID,
            @RequestParam(value = "CID" ,required = false) String CID,
            @RequestParam(value = "DNAME" ,required = false) String DNAME,
            @RequestParam(value = "UNITNAME" ,required = false) String UNITNAME,
            @RequestParam(value = "OAID2" ,required = false) String OAID2,
            @RequestParam(value = "IMEI2" ,required = false) String IMEI2,
            @RequestParam(value = "IDFA2" ,required = false) String IDFA2,
            @RequestParam(value = "MAC2" ,required = false) String MAC2,
            @RequestParam(value = "ANDROIDID2" ,required = false) String ANDROIDID2,
            @RequestParam(value = "UA" ,required = false) String UA,
            @RequestParam(value = "OS" ,required = false) String OS,
            @RequestParam(value = "MODEL" ,required = false) String MODEL,
            @RequestParam(value = "CALLBACK" ,required = false) String CALLBACK,
            @RequestParam(value = "IP" ,required = false)  String IP,
            HttpServletRequest request, HttpServletResponse response
    ) throws IOException {
        log.info("推广相关channle = "+channel);


//        https://bkservice.dyh2024.top/tg/ksCallback?
//        channle=ksaddiction-001
//        &type=2
//        &AID=__AID__
//        &CID=__CID__
//        &DNAME=__DNAME__
//        &UNITNAME=__UNITNAME__
//        &OAID2=__OAID2__
//        &IMEI2=__IMEI2__
//        &IDFA2=__IDFA2__
//        &MAC2=__MAC2__
//        &ANDROIDID2=__ANDROIDID2__
//        &UA=__UA__
//        &OS=__OS__
//        &MODEL=__MODEL__
//        &CALLBACK=__CALLBACK__
//        &IP=__IP__


        TgDeviceKs tgDeviceKs = new TgDeviceKs( DID, AID, CID
                , DNAME, UNITNAME, OAID2, IMEI2, IDFA2
                , MAC2, ANDROIDID2, UA
                , OS, MODEL, CALLBACK, IP);

        apiTgCallBackKsIdService.addTgCallBackKsId(channel ,packChannel,projectId,tgDeviceKs );
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("success");
        response.getWriter().flush();
        response.getWriter().close();
    }



}
